#!/usr/bin/python
#-*-encoding:utf8-*-
import hashlib
import sys,os

def encrypt_md5(str_pre,instr):
    #md5(xw+号码+md5(号码))
    enc_one = hashlib.md5(instr) #step 1
    str_enc_one = enc_one.hexdigest()
    str_enc = hashlib.md5(str_pre+mdn+str_enc_one) #step 2
    str_enc_two = str_enc.hexdigest()
    return str_enc_two

try:
    infile = sys.argv[1]
except:
    print 'please choose file name.'
    sys.exit(1)

if os.path.exists(infile) is False:
    print 'No such file or directory.'
    sys.exit(1)

wf = open('result_'+sys.argv[1],'w')

with open(sys.argv[1],'r') as f:
    mdns = []
    enc_mdns = []
    count = 0
    print 'doing ',sys.argv[1],'...'
    for line in f:
        if line.strip():
            count += 1
            if count%10000==0:
                print count
            mdns = line.replace(',','|').split('|')
            enc_mdns = []
            for mdn in mdns[0:2]:
                mdn = mdn.strip()
                if mdn == '':
                    mdn = ' ' 
                str_md5 = encrypt_md5('xw',mdn)
                enc_mdns.append(str_md5)
            str_wf = enc_mdns[0]+'|'+enc_mdns[1]+'|'+'|'.join(mdns[2:])
            wf.write(str_wf)
wf.close()
print 'done!'
